package com.lanou.myapp.news.funnynews;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.lanou.myapp.R;
import com.lanou.myapp.imageloader.VolleySingleton;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/16.
 */
public class FunnyNewsAdapter extends BaseAdapter{
    private Context context;
    private List<TodayInfo> data;


    public FunnyNewsAdapter(Context context) {
        this.context = context;
//        init();
    }

    public void addData(List<TodayInfo> data){
        this.data = data;
        notifyDataSetChanged();
    }


//    private void init() {
//        queue = Volley.newRequestQueue(context);
//        lruCache = new LruCache<String,Bitmap>(20);
//        imageCache = new ImageLoader.ImageCache() {
//            @Override
//            public Bitmap getBitmap(String url) {
//                return lruCache.get(url);
//            }
//
//            @Override
//            public void putBitmap(String url, Bitmap bitmap) {
//                lruCache.put(url,bitmap);
//            }
//        };
//        imageLoader = new ImageLoader(queue,imageCache);
//    }


//    public void getBitmapCache(String imgUrl, ImageView imageView){
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.img_guide_p1,R.mipmap.img_guide_p1);
//        imageLoader.get(imgUrl,listener,200,200);
//    }

    @Override
    public int getCount() {
        return data.size() > 0 && data != null ? data.size()/2 : 0;
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        MyViewHolder myViewHolder;
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.item_funny_news,null);
            myViewHolder = new MyViewHolder();
            myViewHolder.title = (TextView) view.findViewById(R.id.tv_item_today_title);
            myViewHolder.pic = (ImageView) view.findViewById(R.id.iv_item_today_pic);
            view.setTag(myViewHolder);
        }
        else{
            myViewHolder = (MyViewHolder) view.getTag();
        }
        myViewHolder.title.setText(data.get(i).getTitle());
//        String imgurl = data.get(i).getThumbnail();
//        getBitmapCache(imgurl, myViewHolder.pic);

//        myViewHolder.networkImageView.setDefaultImageResId(R.mipmap.v6_boy_unseletor_icon);
//        myViewHolder.networkImageView.setErrorImageResId(R.mipmap.v5_icon_usercenter);
//        myViewHolder.networkImageView.setImageUrl(data.get(i).getThumbnail(), imageLoader);

//        Glide.with(context).load(data.get(i).getThumbnail()).into(myViewHolder.pic);
        Picasso.with(context).load(data.get(i).getThumbnail()).resize(200,250).into(myViewHolder.pic);

        return view;
    }

    private class MyViewHolder{
        TextView title;
        ImageView pic;
        NetworkImageView networkImageView;
    }

}
